'''
Created on 2011-07-17

@author: Mikael
'''
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from View import AppStyleSheet


class TextWidget(QTextEdit):
    
    def __init__(self, text, MiniTextEntryWidget):
        QTextEdit.__init__(self, text)
        self.miniText = MiniTextEntryWidget
         
    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_Return:
            self.miniText.close()
        else:
            QTextEdit.keyPressEvent(self, QKeyEvent)
            
    def focusOutEvent(self, QFocusEvent):
        self.setPlainText("")
        self.miniText.close()
        

class MiniTextEntryWidget(QWidget):
    '''
    classdocs
    '''
    
    mSignalReturned = pyqtSignal()

    def __init__(self, defaultText="", parent=None):
        '''
        Constructor
        '''
        
        #QWidget.__init__(self)
        super(MiniTextEntryWidget, self).__init__()
        self.setParent(parent)
        
        self.mDefaultText = defaultText
        
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint |  Qt.SplashScreen )
        self.setAttribute(Qt.WA_TranslucentBackground);
        
        self.mSupHeight = 100
        self.mSupWidth = 100
        self.mBaseWidth = 100

        self.mLayout = QVBoxLayout()
        self.setLayout(self.mLayout)
        self.mLayout.setMargin(10)
        self.mLayout.setSpacing(10)
        
        
        self.mTextBox = TextWidget(defaultText, self)
        self.mTextBox.resize(100,100)
        
    
        self.mTextBox.setWordWrapMode(QTextOption.NoWrap);
        self.mTextBox.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff);
        self.mTextBox.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff);
        self.mTextBox.move(self.mSupWidth / 2 ,self.mSupHeight / 2)
        metrics =  QFontMetrics(self.mTextBox.font())
        rowHeight = metrics.lineSpacing()
        final = rowHeight + 6
        self.mTextBox.setFixedHeight (rowHeight + 6) 
        
        self.mLayout.addChildWidget(self.mTextBox)
        self.resize(self.mBaseWidth + self.mSupWidth,final + self.mSupHeight)
        
        self.setStyleSheet(AppStyleSheet.mStyleSheet)
        
    def close(self):
        self.mSignalReturned.emit()
        self.hide()
        
    def spawn(self, x, y):
        self.move(QPoint(x - self.width()/2, y - self.height()/2))
        self.mTextBox.setText(self.mDefaultText)
        self.mTextBox.selectAll()
        self.show()
        
    def getText(self):
        return self.mTextBox.toPlainText()
    

        
    def paintEvent(self, QPaintEvent):
        
        painter =  QPainter(self);
        painter.setRenderHint(QPainter.Antialiasing)
        
        
        
        '''linGrad = QLinearGradient(self.width() / 2, self.height() / 2, self.width() / 2, 0)
        linGrad.setColorAt(0,QColor(0,0,0, 255))
        linGrad.setColorAt(0.8,QColor(100,100,100, 200))
        linGrad.setColorAt(1.0,QColor(150,150,150, 0))
        linGrad.setSpread(QGradient.ReflectSpread)
        painter.fillRect(self.mSupWidth / 2, 0, self.width() - self.mSupWidth, self.height(),linGrad)
        '''
        
        '''
        gradientRadial = QRadialGradient();
        gradientRadial.setCenter(self.mSupWidth / 2, self.height() / 2);
        gradientRadial.setFocalPoint(self.mSupWidth / 2, self.height() / 2)
        gradientRadial.setRadius(self.height() / 2)
        linGrad.setColorAt(0,QColor(0,0,0, 255))
        linGrad.setColorAt(0.8,QColor(200,200,200, 200))
        linGrad.setColorAt(1.0,QColor(255,255,255, 0))
        painter.setBrush(gradientRadial)
        painter.fillRect(0, 0, self.mSupWidth / 2, self.height(),gradientRadial)
        '''
        